<HTML>

<HEAD>
        <META NAME="keywords" CONTENT="electronic commerce, ecommerce, ebusiness, e-business, e-commerce, enterprise software, net economy, Netscape, Sun Microsystems, Sun-Netscape Alliance, iPlanet, internet software">
        <META NAME="description" CONTENT="The Sun-Netscape Alliance, a strategic alliance formed by America Online and Sun Microsystems, Inc., is delivering iPlanet e-commerce software and enterprise solutions that enable companies to compete successfully in the Net Economy">
	<META HTTP-EQUIV="Content-Type" content="text/html; charset=ISO-8859-1">
	<META NAME="TEMPLATEBASE" CONTENT="Authored in FrameMaker. Converted to HTML in WebWorks Publisher 2000. manual wdt 1.5">
	<META NAME="LASTUPDATED" CONTENT="09/28/00 13:17:54">
	<TITLE> Core JavaScript Guide 1.5: 2 Values, Variables, and Literals</TITLE>
</HEAD>




<BODY TEXT="#000000" LINK="#666699" VLINK="#666699" ALINK="#333366" BGCOLOR="#FFFFFF">

<MAINCONTENT>

<NAVIGATIONCONTENT>
   <TABLE WIDTH="100%" BORDER="0" CELLSPACING="4">
      <TR>
         <TD VALIGN="TOP">
            <b><a href="partcore.html">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Previous</a></FONT>&nbsp;&nbsp;&nbsp;&nbsp;</b>
<a href="contents.html">
<b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Contents</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="ix.html"><b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Index</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="expr.html">
<b>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">
Next
</a>
</FONT>
</b>&nbsp;&nbsp;&nbsp;&nbsp;
         </TD>
     </TR>

      <TR>
         <TD VALIGN="TOP" ALIGN="RIGHT" BGCOLOR="#CCCCCC">
            <FONT FACE="Arial, Helvetica, sans-serif" COLOR="#000000" SIZE="2"><B>
             Core JavaScript Guide 1.5</B></FONT>
         </TD>
      </TR>
      <TR>
   </TABLE>
   <BR>
</NAVIGATIONCONTENT>

<BOOKCONTENT>
   <blockquote>
<BR>
<BR>

      <A NAME="1008300"> </A><A NAME=""> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="4" COLOR="#666666"><B>
<zchapnumber>Chapter 2 </zchapnumber>
&nbsp;
</B>
</FONT>


<A NAME="1008302"> </A>
<A NAME="Values, Variables, and Literals"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4" COLOR="#000000">
<B>
<zchaptitle>Values, Variables, and Literals</zchaptitle>
</B>
</FONT>
<BR>
<BR>
<BR>

<A NAME="1008304"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This chapter discusses values that JavaScript recognizes and describes the fundamental building blocks of JavaScript expressions: variables, constants, and literals.</zparagraph>
</FONT>
<P>

<A NAME="1009896"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This chapter contains the following sections:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1009897"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="ident.html#1008306"  >Values</A></zsmartlist1>
</FONT>
<P><A NAME="1009986"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="ident.html#1008330"  >Variables</A></zsmartlist1>
</FONT>
<P><A NAME="1012977"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="ident.html#1012963"  >Constants</A></zsmartlist1>
</FONT>
<P><A NAME="1010226"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="ident.html#1013090"  >Literals</A></zsmartlist1>
</FONT>
<P><A NAME="1010232"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="ident.html#1009568"  >Unicode</A></zsmartlist1>
</FONT>
<P></ul>

<BR>
<BR>

<A NAME="1008306"> </A>
<A NAME="Values"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead1>Values</zhead1>
<HR SIZE="2" NOSHADE>
</FONT>
<BR>


<A NAME="1008308"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>JavaScript recognizes the following types of values:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1008309"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>Numbers, such as 42 or 3.14159</zsmartlist1>
</FONT>
<P><A NAME="1008310"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>Logical (Boolean) values, either <FONT FACE="courier, couriernew, monospaced size">true</FONT> or <FONT FACE="courier, couriernew, monospaced size">false</FONT></zsmartlist1>
</FONT>
<P><A NAME="1008311"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>Strings, such as "Howdy!"</zsmartlist1>
</FONT>
<P><A NAME="1008312"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">null</FONT>, a special keyword denoting a null value; <FONT FACE="courier, couriernew, monospaced size">null</FONT> is also a primitive value. Because JavaScript is case-sensitive, <FONT FACE="courier, couriernew, monospaced size">null</FONT> is not the same as <FONT FACE="courier, couriernew, monospaced size">Null</FONT>, <FONT FACE="courier, couriernew, monospaced size">NULL</FONT>, or any other variant</zsmartlist1>
</FONT>
<P><A NAME="1009742"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">undefined</FONT>, a top-level property whose value is undefined; <FONT FACE="courier, couriernew, monospaced size">undefined</FONT> is also a primitive value</zsmartlist1>
</FONT>
<P></ul>

<A NAME="1008315"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This relatively small set of types of values, or <I>data types</I>, enables you to perform useful functions with your applications. There is no explicit distinction between integer and real-valued numbers. Nor is there an explicit date data type in JavaScript. However, you can use the <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object and its methods to handle dates.</zparagraph>
</FONT>
<P>

<A NAME="1008317"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Objects and functions are the other fundamental elements in the language. You can think of objects as named containers for values, and functions as procedures that your application can perform.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008319"> </A>
<A NAME="Data Type Conversion"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Data Type Conversion</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008320"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>JavaScript is a dynamically typed language. That means you do not have to specify the data type of a variable when you declare it, and data types are converted automatically as needed during script execution. So, for example, you could define a variable as follows:</zparagraph>
</FONT>
<P>
<A NAME="1008321"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>var answer = 42
</zcodeline>
</FONT>
<P>
<A NAME="1008322"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>And later, you could assign the same variable a string value, for example:</zparagraph>
</FONT>
<P>
<A NAME="1008323"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>answer = "Thanks for all the fish..."
</zcodeline>
</FONT>
<P>
<A NAME="1008324"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Because JavaScript is dynamically typed, this assignment does not cause an error message.</zparagraph>
</FONT>
<P>

<A NAME="1008325"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>In expressions involving numeric and string values with the + operator, JavaScript converts numeric values to strings. For example, consider the following statements:</zparagraph>
</FONT>
<P>
<A NAME="1008326"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>x = "The answer is " + 42 // returns "The answer is 42"<br>
y = 42 + " is the answer" // returns "42 is the answer"
</zcodeline>
</FONT>
<P>
<A NAME="1008327"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>In statements involving other operators, JavaScript does not convert numeric values to strings. For example:</zparagraph>
</FONT>
<P>
<A NAME="1011514"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>"37" - 7 // returns 30<br>
"37" + 7 // returns 377
</zcodeline>
</FONT>
<P>
<BR>
<BR>

<A NAME="1008330"> </A>
<A NAME="Variables"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead1>Variables</zhead1>
<HR SIZE="2" NOSHADE>
</FONT>
<BR>


<A NAME="1008331"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You use variables as symbolic names for values in your application. The names of variables, called <I>identifiers</I>, conform to certain rules.</zparagraph>
</FONT>
<P>

<A NAME="1008333"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).</zparagraph>
</FONT>
<P>

<A NAME="1013333"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Starting with JavaScript 1.5, you can use ISO 8859-1 or Unicode letters such as &aring; and &uuml; in identifiers. You can also use the \uXXXX Unicode escape sequences listed on <A HREF="ident.html#1009576"  >page&nbsp;34</A> as characters in identifiers.</zparagraph>
</FONT>
<P>

<A NAME="1013366"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Some examples of legal names are <FONT FACE="courier, couriernew, monospaced size">Number_hits</FONT>, <FONT FACE="courier, couriernew, monospaced size">temp99</FONT>, and <FONT FACE="courier, couriernew, monospaced size">_name</FONT>.</zparagraph>
</FONT>
<P>

<A NAME="1013326"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph></zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008338"> </A>
<A NAME="Declaring Variables"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Declaring Variables</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008339"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can declare a variable in two ways:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1008340"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>By simply assigning it a value. For example, <FONT FACE="courier, couriernew, monospaced size">x = 42</FONT></zsmartlist1>
</FONT>
<P><A NAME="1008342"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>With the keyword <FONT FACE="courier, couriernew, monospaced size">var.</FONT> For example, <FONT FACE="courier, couriernew, monospaced size">var x = 42</FONT></zsmartlist1>
</FONT>
<P></ul>
<P>
<BR>
<A NAME="1012659"> </A>
<A NAME="Evaluating Variables"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Evaluating Variables</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1013184"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A variable or array element that has not been assigned a value has the value <FONT FACE="courier, couriernew, monospaced size">undefined</FONT>. The result of evaluating an unassigned variable depends on how it was declared:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1013185"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>If the unassigned variable was declared without <FONT FACE="courier, couriernew, monospaced size">var</FONT>, the evaluation results in a runtime error.</zsmartlist1>
</FONT>
<P><A NAME="1012441"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>If the unassigned variable was declared with <FONT FACE="courier, couriernew, monospaced size">var</FONT>, the evaluation results in the undefined value, or NaN in numeric contexts.</zsmartlist1>
</FONT>
<P></ul>

<A NAME="1012776"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following code demonstrates evaluating unassigned variables.</zparagraph>
</FONT>
<P>
<A NAME="1012304"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function f1() {<br>
&nbsp;&nbsp;&nbsp;return y - 2;<br>
}<br>
f1() //Causes runtime error
</zcodeline>
</FONT>
<P><A NAME="1012316"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function f2() {<br>
&nbsp;&nbsp;&nbsp;return var y - 2;<br>
}<br>
f2() //returns NaN
</zcodeline>
</FONT>
<P>
<A NAME="1012314"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can use <FONT FACE="courier, couriernew, monospaced size">undefined</FONT> to determine whether a variable has a value. In the following code, the variable <FONT FACE="courier, couriernew, monospaced size">input</FONT> is not assigned a value, and the <FONT FACE="courier, couriernew, monospaced size">if</FONT> statement evaluates to <FONT FACE="courier, couriernew, monospaced size">true</FONT>.</zparagraph>
</FONT>
<P>
<A NAME="1009807"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>var input;<br>
if(input === undefined){<br>
&nbsp;&nbsp;&nbsp;doThis();<br>
} else {<br>
<FONT FACE="courier, couriernew, monospaced size">&nbsp;&nbsp;&nbsp;doThat();<br>
</FONT>}
</zcodeline>
</FONT>
<P>
<A NAME="1012352"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">undefined</FONT> value behaves as false when used as a Boolean value. For example, the following code executes the function <FONT FACE="courier, couriernew, monospaced size">myFunction</FONT> because the array element is not defined:</zparagraph>
</FONT>
<P>
<A NAME="1012353"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>myArray=new Array()<br>
if (!myArray[0])<br>
&nbsp;&nbsp;&nbsp;myFunction()
</zcodeline>
</FONT>
<P>
<A NAME="1012381"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>When you evaluate a null variable, the null value behaves as 0 in numeric contexts and as false in Boolean contexts. For example:</zparagraph>
</FONT>
<P>
<A NAME="1012678"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>var n = null<br>
n * 32 //returns 0
</zcodeline>
</FONT>
<P><P>
<BR>
<A NAME="1009822"> </A>
<A NAME="Variable Scope"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Variable Scope</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008343"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>When you set a variable identifier by assignment outside of a function, it is called a <I>global</I> variable, because it is available everywhere in the current document. When you declare a variable within a function, it is called a <I>local</I> variable, because it is available only within the function.</zparagraph>
</FONT>
<P>

<A NAME="1008344"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Using <FONT FACE="courier, couriernew, monospaced size">var</FONT> to declare a global variable is optional. However, you must use <FONT FACE="courier, couriernew, monospaced size">var</FONT> to declare a variable inside a function.</zparagraph>
</FONT>
<P>

<A NAME="1008345"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable called <FONT FACE="courier, couriernew, monospaced size">phoneNumber</FONT> is declared in a <FONT FACE="courier, couriernew, monospaced size">FRAMESET</FONT> document, you can refer to this variable from a child frame as <FONT FACE="courier, couriernew, monospaced size">parent.phoneNumber</FONT>.</zparagraph>
</FONT>
<P>

<BR>
<BR>

<A NAME="1012963"> </A>
<A NAME="Constants"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead1>Constants</zhead1>
<HR SIZE="2" NOSHADE>
</FONT>
<BR>


<A NAME="1012982"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can create a read-only, named constant with the <FONT FACE="courier, couriernew, monospaced size">const</FONT> keyword. The syntax of a constant identifier is the same as for a variable identifier: it must start with a letter or underscore and can contain alphabetic, numeric, or underscore characters.</zparagraph>
</FONT>
<P>
<A NAME="1013045"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>const prefix = '212';
</zcodeline>
</FONT>
<P>
<A NAME="1013035"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A constant cannot change value through assignment or be re-declared while the script is running.</zparagraph>
</FONT>
<P>

<A NAME="1013052"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The scope rules for constants are the same as those for variables, except that the <FONT FACE="courier, couriernew, monospaced size">const</FONT> keyword is always required, even for global constants. If the keyword is omitted, the identifier is assumed to represent a <FONT FACE="courier, couriernew, monospaced size">var</FONT>.</zparagraph>
</FONT>
<P>

<A NAME="1013057"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You cannot declare a constant at the same scope as a function or variable with the same name as the function or variable. For example:</zparagraph>
</FONT>
<P>
<A NAME="1013058"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>//THIS WILL CAUSE AN ERROR<br>
function f{};<br>
const f = 5;
</zcodeline>
</FONT>
<P><A NAME="1013096"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>
</zcodeline>
</FONT>
<P><A NAME="1013086"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>//THIS WILL CAUSE AN ERROR ALSO<br>
function f{<br>
const g=5;<br>
var g;
</zcodeline>
</FONT>
<P><A NAME="1013087"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>//statements
</zcodeline>
</FONT>
<P><A NAME="1013095"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>}
</zcodeline>
</FONT>
<P>
<BR>
<BR>

<A NAME="1013090"> </A>
<A NAME="Literals"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead1>Literals</zhead1>
<HR SIZE="2" NOSHADE>
</FONT>
<BR>


<A NAME="1008349"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You use literals to represent values in JavaScript. These are fixed values, not variables, that you <I>literally</I> provide in your script. This section describes the following types of literals:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1009486"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="ident.html#1011655"  >Array Literals</A></zsmartlist1>
</FONT>
<P><A NAME="1009494"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="ident.html#1012095"  >Boolean Literals</A></zsmartlist1>
</FONT>
<P><A NAME="1009505"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="ident.html#1009427"  >Floating-Point Literals</A></zsmartlist1>
</FONT>
<P><A NAME="1009506"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="ident.html#1008351"  >Integers</A></zsmartlist1>
</FONT>
<P><A NAME="1009544"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="ident.html#1009450"  >Object Literals</A></zsmartlist1>
</FONT>
<P><A NAME="1011652"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="ident.html#1008368"  >String Literals</A></zsmartlist1>
</FONT>
<P></ul>
<P>
<BR>
<A NAME="1011655"> </A>
<A NAME="Array Literals"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Array Literals</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1011596"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>An array literal is a list of zero or more expressions, each of which represents an array element, enclosed in square brackets ([]). When you create an array using an array literal, it is initialized with the specified values as its elements, and its length is set to the number of arguments specified.</zparagraph>
</FONT>
<P>

<A NAME="1012183"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following example creates the <FONT FACE="courier, couriernew, monospaced size">coffees</FONT> array with three elements and a length of three:</zparagraph>
</FONT>
<P>
<A NAME="1012184"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>coffees = ["French Roast", "Columbian", "Kona"]
</zcodeline>
</FONT>
<P>
<A NAME="1012185"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<znote>Note</znote>
</B>
</FONT>


<A NAME="1012142"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>An array literal is a type of object initializer. See <A HREF="obj.html#1008330"  >"Using Object Initializers" on page&nbsp;93</A>.</zparagraph>
</FONT>
<P>

<A NAME="1011913"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>If an array is created using a literal in a top-level script, JavaScript interprets the array each time it evaluates the expression containing the array literal. In addition, a literal used in a function is created each time the function is called.</zparagraph>
</FONT>
<P>

<A NAME="1012089"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Array literals are also <FONT FACE="courier, couriernew, monospaced size">Array</FONT> objects. See <A HREF="obj.html#1008453"  >"Array Object" on page&nbsp;100</A> for details on <FONT FACE="courier, couriernew, monospaced size">Array</FONT> objects.</zparagraph>
</FONT>
<P>
<BR>
<P>
<A NAME="1012099"> </A>
<A NAME="Extra Commas in Array Literals"> </A>
<FONT FACE="Arial, Helvetica, sans-serif">
<B>
<zhead3>Extra Commas in Array Literals</zhead3>
</B>
</FONT>
<BR>
<BR>

<A NAME="1012158"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You do not have to specify all elements in an array literal. If you put two commas in a row, the array is created with spaces for the unspecified elements. The following example creates the <FONT FACE="courier, couriernew, monospaced size">fish</FONT> array:</zparagraph>
</FONT>
<P>
<A NAME="1012159"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>fish = ["Lion", , "Angel"]
</zcodeline>
</FONT>
<P>
<A NAME="1012160"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This array has two elements with values and one empty element (<FONT FACE="courier, couriernew, monospaced size">fish[0]</FONT> is "Lion", <FONT FACE="courier, couriernew, monospaced size">fish[1]</FONT> is undefined, and <FONT FACE="courier, couriernew, monospaced size">fish[2]</FONT> is "Angel"):</zparagraph>
</FONT>
<P>

<A NAME="1012163"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>If you include a trailing comma at the end of the list of elements, the comma is ignored. In the following example, the length of the array is three. There is no <FONT FACE="courier, couriernew, monospaced size">myList[3]</FONT>. All other commas in the list indicate a new element.</zparagraph>
</FONT>
<P>
<A NAME="1012105"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>myList = ['home', , 'school', ];
</zcodeline>
</FONT>
<P>
<A NAME="1012106"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>In the following example, the length of the array is four, and <FONT FACE="courier, couriernew, monospaced size">myList[0]</FONT> and <FONT FACE="courier, couriernew, monospaced size">myList[2]</FONT>are missing.</zparagraph>
</FONT>
<P>
<A NAME="1012107"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>myList = [ , 'home', , 'school'];
</zcodeline>
</FONT>
<P>
<A NAME="1012108"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>In the following example, the length of the array is four, and <FONT FACE="courier, couriernew, monospaced size">myList[1]</FONT>and <FONT FACE="courier, couriernew, monospaced size">myList[3]</FONT>are missing. Only the last comma is ignored. This trailing comma is optional.</zparagraph>
</FONT>
<P>
<A NAME="1012109"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>myList = ['home', , 'school', , ];
</zcodeline>
</FONT>
<P><P>
<BR>
<A NAME="1012095"> </A>
<A NAME="Boolean Literals"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Boolean Literals</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1012096"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The Boolean type has two literal values: <FONT FACE="courier, couriernew, monospaced size">true</FONT> and <FONT FACE="courier, couriernew, monospaced size">false</FONT>.</zparagraph>
</FONT>
<P>

<A NAME="1012264"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object. The Boolean object is a wrapper around the primitive Boolean data type. See <A HREF="obj.html#1008502"  >"Boolean Object" on page&nbsp;103</A> for more information.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1009427"> </A>
<A NAME="Floating-Point Literals"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Floating-Point Literals</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1009428"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A floating-point literal can have the following parts:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1009429"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>A decimal integer</zsmartlist1>
</FONT>
<P><A NAME="1009430"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>A decimal point (".")</zsmartlist1>
</FONT>
<P><A NAME="1009431"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>A fraction (another decimal number)</zsmartlist1>
</FONT>
<P><A NAME="1009432"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>An exponent</zsmartlist1>
</FONT>
<P></ul>

<A NAME="1009434"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The exponent part is an "e" or "E" followed by an integer, which can be signed (preceded by "+" or "-"). A floating-point literal must have at least one digit and either a decimal point or "e" (or "E").</zparagraph>
</FONT>
<P>

<A NAME="1009435"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Some examples of floating-point literals are 3.1415, -3.1E12, .1e12, and 2E-12.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008351"> </A>
<A NAME="Integers"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Integers</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008352"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Integers can be expressed in decimal (base 10), hexadecimal (base 16), and octal (base 8). A decimal integer literal consists of a sequence of digits without a leading 0 (zero). A leading 0 (zero) on an integer literal indicates it is in octal; a leading 0x (or 0X) indicates hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F. Octal integers can include only the digits 0-7.</zparagraph>
</FONT>
<P>

<A NAME="1013394"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Octal integer literals are deprecated and have been removed from the ECMA-262, Edition 3 standard. JavaScript 1.5 still supports them for backward compatibility.</zparagraph>
</FONT>
<P>

<A NAME="1008353"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Some examples of integer literals are: 42, 0xFFF, and -345.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1009450"> </A>
<A NAME="Object Literals"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Object Literals</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1011679"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). You should not use an object literal at the beginning of a statement. This will lead to an error or not behave as you expect, because the { will be interpreted as the beginning of a block.</zparagraph>
</FONT>
<P>

<A NAME="1009452"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following is an example of an object literal. The first element of the <FONT FACE="courier, couriernew, monospaced size">car</FONT> object defines a property, <FONT FACE="courier, couriernew, monospaced size">myCar</FONT>; the second element, the <FONT FACE="courier, couriernew, monospaced size">getCar</FONT> property, invokes a function (<FONT FACE="courier, couriernew, monospaced size">CarTypes("Honda")</FONT>); the third element, the <FONT FACE="courier, couriernew, monospaced size">special</FONT> property, uses an existing variable (<FONT FACE="courier, couriernew, monospaced size">Sales</FONT>).</zparagraph>
</FONT>
<P>
<A NAME="1009453"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>var Sales = "Toyota";
</zcodeline>
</FONT>
<P><A NAME="1009454"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function CarTypes(name) {<br>
&nbsp;&nbsp;&nbsp;if(name == "Honda")<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return name;<br>
&nbsp;&nbsp;&nbsp;else<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "Sorry, we don't sell " + name + ".";<br>
}
</zcodeline>
</FONT>
<P><A NAME="1009455"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>car = {myCar: "Saturn", getCar: CarTypes("Honda"), special: Sales}
</zcodeline>
</FONT>
<P><A NAME="1009456"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>document.write(car.myCar); // Saturn<br>
document.write(car.getCar); // Honda<br>
document.write(car.special); // Toyota
</zcodeline>
</FONT>
<P>
<A NAME="1009457"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Additionally, you can use a numeric or string literal for the name of a property or nest an object inside another. The following example uses these options. </zparagraph>
</FONT>
<P>
<A NAME="1009458"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>car = {manyCars: {a: "Saab", b: "Jeep"}, 7: "Mazda"}
</zcodeline>
</FONT>
<P><A NAME="1009459"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>document.write(car.manyCars.b); // Jeep<br>
document.write(car[7]); // Mazda
</zcodeline>
</FONT>
<P><P>
<BR>
<A NAME="1008368"> </A>
<A NAME="String Literals"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>String Literals</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008369"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A string literal is zero or more characters enclosed in double (<FONT FACE="courier, couriernew, monospaced size">"</FONT>) or single (<FONT FACE="courier, couriernew, monospaced size">'</FONT>) quotation marks. A string must be delimited by quotation marks of the same type; that is, either both single quotation marks or both double quotation marks. The following are examples of string literals:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1008371"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">"blah"</FONT></zsmartlist1>
</FONT>
<P><A NAME="1008372"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">'blah'</FONT></zsmartlist1>
</FONT>
<P><A NAME="1008373"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">"1234"</FONT></zsmartlist1>
</FONT>
<P><A NAME="1008374"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">"one line \n another line"</FONT></zsmartlist1>
</FONT>
<P></ul>

<A NAME="1012232"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can call any of the methods of the String object on a string literal value&#151;JavaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object. You can also use the <FONT FACE="courier, couriernew, monospaced size">String.length</FONT> property with a string literal.</zparagraph>
</FONT>
<P>

<A NAME="1011730"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You should use string literals unless you specifically need to use a String object. See <A HREF="obj.html#1008731"  >"String Object" on page&nbsp;110</A> for details on <FONT FACE="courier, couriernew, monospaced size">String</FONT> objects.</zparagraph>
</FONT>
<P>
<BR>
<P>
<A NAME="1011693"> </A>
<A NAME="Using Special Characters in Strings"> </A>
<FONT FACE="Arial, Helvetica, sans-serif">
<B>
<zhead3>Using Special Characters in Strings</zhead3>
</B>
</FONT>
<BR>
<BR>

<A NAME="1008375"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>In addition to ordinary characters, you can also include special characters in strings, as shown in the following example.</zparagraph>
</FONT>
<P>
<A NAME="1012199"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>"one line \n another line"
</zcodeline>
</FONT>
<P>
<A NAME="1012197"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following table lists the special characters that you can use in JavaScript strings.</zparagraph>
</FONT>
<P>

<P>
<A NAME="1009867"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<BR>
<P>
<TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0">
  <CAPTION></CAPTION><P><FONT FACE=""><B>
<A NAME="1008383"> </A>
<A NAME="JavaScript special characters "> </A>
<ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablecaption>Table 2.1&#32;&nbsp&nbsp JavaScript special characters </ztablecaption>
</B>
</FONT>
</B></FONT>
<BR>
<BR>
  <TR BGCOLOR="#CCCCCC" ALIGN=LEFT VALIGN=TOP>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1008387"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Character</ztablehead>
</B>
</FONT>
</TH>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1008389"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Meaning</ztablehead>
</B>
</FONT>
</TH>
  </DIV></TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008391"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>\b
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008393"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Backspace</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008395"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>\f
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008397"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Form feed</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008399"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>\n
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008401"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>New line</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008403"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>\r
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008405"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Carriage return</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008407"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>\t
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008409"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Tab</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1013407"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>\v
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1013409"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Vertical tab</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1012500"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>\'
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1012502"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Apostrophe or single quote</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1012496"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>\"
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1012498"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Double quote</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008411"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>\\
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008413"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Backslash character (\).</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1012526"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>\<I>XXX
</I></zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1012528"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>The character with the Latin-1 encoding specified by up to three octal digits <I>XXX</I> between 0 and 377. For example, \251 is the octal sequence for the copyright symbol.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1012522"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>\x<I>XX
</I></zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1012524"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>The character with the Latin-1 encoding specified by the two hexadecimal digits <I>XX</I> between 00 and FF. For example, \xA9 is the hexadecimal sequence for the copyright symbol.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1012569"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>\u<I>XXXX
</I></zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1012571"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>The Unicode character specified by the four hexadecimal digits <I>XXXX</I>. For example, \u00A9 is the Unicode sequence for the copyright symbol. See <A HREF="ident.html#1009576"  >Unicode Escape Sequences</A>.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
</TABLE>



<P>
</FONT>
<P>

<BR>
<P>
<A NAME="1008415"> </A>
<A NAME="Escaping Characters"> </A>
<FONT FACE="Arial, Helvetica, sans-serif">
<B>
<zhead3>Escaping Characters</zhead3>
</B>
</FONT>
<BR>
<BR>

<A NAME="1008419"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>For characters not listed in <A HREF="ident.html#1008383"  >Table 2.1</A>, a preceding backslash is ignored, but this usage is deprecated and should be avoided.</zparagraph>
</FONT>
<P>

<A NAME="1008420"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can insert a quotation mark inside a string by preceding it with a backslash. This is known as <I>escaping</I> the quotation mark. For example,</zparagraph>
</FONT>
<P>
<A NAME="1008421"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service."<br>
document.write(quote)
</zcodeline>
</FONT>
<P>
<A NAME="1008422"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The result of this would be</zparagraph>
</FONT>
<P>
<dl>  <dl>
     <DT> <A NAME="1008423"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<zparaindent1>He read "The Cremation of Sam McGee" by R.W. Service.</zparaindent1>
</FONT>
<P>  </dl>
</dl>
<A NAME="1008424"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>To include a literal backslash inside a string, you must escape the backslash character. For example, to assign the file path <FONT FACE="courier, couriernew, monospaced size">c:\temp</FONT> to a string, use the following:</zparagraph>
</FONT>
<P>
<A NAME="1008425"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>var home = <FONT FACE="courier, couriernew, monospaced size">"</FONT>c:\\temp<FONT FACE="courier, couriernew, monospaced size">"
</FONT></zcodeline>
</FONT>
<P>
<BR>
<BR>

<A NAME="1009568"> </A>
<A NAME="Unicode"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead1>Unicode</zhead1>
<HR SIZE="2" NOSHADE>
</FONT>
<BR>


<A NAME="1012482"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Unicode is a universal character-coding standard for the interchange and display of principal written languages. It covers the languages of Americas, Europe, Middle East, Africa, India, Asia, and Pacifica, as well as historic scripts and technical symbols. Unicode allows for the exchange, processing, and display of multilingual texts, as well as the use of common technical and mathematical symbols. It hopes to resolve internationalization problems of multilingual computing, such as different national character standards. Not all modern or archaic scripts, however, are currently supported.</zparagraph>
</FONT>
<P>

<A NAME="1009570"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The Unicode character set can be used for all known encoding. Unicode is modeled after the ASCII (American Standard Code for Information Interchange) character set. It uses a numerical value and name for each character. The character encoding specifies the identity of the character and its numeric value (code position), as well as the representation of this value in bits. The 16-bit numeric value (code value) is defined by a hexadecimal number and a prefix U, for example, U+0041 represents A. The unique name for this value is LATIN CAPITAL LETTER A.</zparagraph>
</FONT>
<P>
<P>
<A NAME="1011422"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<zheadrunin>JavaScript versions prior to 1.3. </zheadrunin>
</B>
</FONT>

<A NAME="1011423"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Unicode is not supported in versions of JavaScript prior to 1.3.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1009571"> </A>
<A NAME="Unicode Compatibility with ASCII and ISO"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Unicode Compatibility with ASCII and ISO</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1009572"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Unicode is compatible with ASCII characters and is supported by many programs. The first 128 Unicode characters correspond to the ASCII characters and have the same byte value. The Unicode characters U+0020 through U+007E are equivalent to the ASCII characters 0x20 through 0x7E. Unlike ASCII, which supports the Latin alphabet and uses a 7-bit character set, Unicode uses a 16-bit value for each character. It allows for tens of thousands of characters. It also supports an extension mechanism, UTF-16, that allows for the encoding of one million more characters by using 16-bit character pairs. UTF turns the encoding to actual bits.</zparagraph>
</FONT>
<P>

<A NAME="1009573"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Unicode is fully compatible with the International Standard ISO/IEC 10646-1; 1993, which is a subset of ISO 10646, and supports the ISO UCS-2 (Universal Character Set) that uses two-octets (two bytes or 16 bits).</zparagraph>
</FONT>
<P>

<A NAME="1009574"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>JavaScript and Navigator support for Unicode means you can use non-Latin, international, and localized characters, plus special technical symbols in JavaScript programs. Unicode provides a standard way to encode multilingual text. Since Unicode is compatible with ASCII, programs can use ASCII characters. You can use non-ASCII Unicode characters in the comments, string literals, identifiers, and regular expressions of JavaScript.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1009576"> </A>
<A NAME="Unicode Escape Sequences"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Unicode Escape Sequences</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1009577"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can use the Unicode escape sequence in string literals, regular expressions, and identifiers. The escape sequence consists of six ASCII characters: \u and a four-digit hexadecimal number. For example, \u00A9 represents the copyright symbol. Every Unicode escape sequence in JavaScript is interpreted as one character.</zparagraph>
</FONT>
<P>

<A NAME="1009580"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following code returns the copyright symbol and the string "Netscape Communications".</zparagraph>
</FONT>
<P>
<A NAME="1009581"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>x="\u00A9 Netscape Communications"
</zcodeline>
</FONT>
<P>
<A NAME="1009584"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following table lists frequently used special characters and their Unicode value.</zparagraph>
</FONT>
<P>

<P>
<A NAME="1009883"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<BR>
<P>
<TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0">
  <CAPTION></CAPTION><P><FONT FACE=""><B>
<A NAME="1009587"> </A>
<A NAME="Unicode values for special characters"> </A>
<ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablecaption>Table 2.2&#32;&nbsp&nbsp Unicode values for special characters</ztablecaption>
</B>
</FONT>
</B></FONT>
<BR>
<BR>
  <TR BGCOLOR="#CCCCCC" ALIGN=LEFT VALIGN=TOP>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1009595"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Category</ztablehead>
</B>
</FONT>
</TH>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1009597"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Unicode value</ztablehead>
</B>
</FONT>
</TH>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1009599"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Name</ztablehead>
</B>
</FONT>
</TH>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1009601"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Format name</ztablehead>
</B>
</FONT>
</TH>
  </DIV></TR>
  <TR>
    <TD VALIGN=TOP>
<P>
<A NAME="1009603"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>White space values</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009605"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>\u0009</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009607"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Tab</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009609"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>&lt;TAB&gt;</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP>
<P>
<A NAME="1009611"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext></ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009613"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>\u000B</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009615"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Vertical Tab</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009617"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>&lt;VT&gt;</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP>
<P>
<A NAME="1009619"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext></ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009621"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>\u000C</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009623"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Form Feed</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009625"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>&lt;FF&gt;</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP>
<P>
<A NAME="1009627"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext></ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009629"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>\u0020</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009631"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Space</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009633"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>&lt;SP&gt;</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP>
<P>
<A NAME="1009635"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Line terminator values</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009637"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>\u000A</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009639"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Line Feed</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009641"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>&lt;LF&gt;</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP>
<P>
<A NAME="1009643"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext></ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009645"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>\u000D</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009647"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Carriage Return</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009649"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>&lt;CR&gt;</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP>
<P>
<A NAME="1009651"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Additional Unicode escape sequence values</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009653"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>\u0008</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009655"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Backspace</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009657"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>&lt;BS&gt;</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP>
<P>
<A NAME="1009659"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext></ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009661"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>\u0009</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009663"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Horizontal Tab</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009665"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>&lt;HT&gt;</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP>
<P>
<A NAME="1009667"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext></ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009669"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>\u0022</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009671"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Double Quote</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009673"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>"</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP>
<P>
<A NAME="1009675"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext></ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009677"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>\u0027</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009679"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Single Quote</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009681"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>'</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP>
<P>
<A NAME="1009683"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext></ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009685"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>\u005C</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009687"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Backslash</ztabletext>
</FONT>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1009689"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>\</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
</TABLE>



<P>
</FONT>
<P>


<A NAME="1012604"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The JavaScript use of the Unicode escape sequence is different from Java. In JavaScript, the escape sequence is never interpreted as a special character first. For example, a line terminator escape sequence inside a string does not terminate the string before it is interpreted by the function. JavaScript ignores any escape sequence if it is used in comments. In Java, if an escape sequence is used in a single comment line, it is interpreted as an Unicode character. For a string literal, the Java compiler interprets the escape sequences first. For example, if a line terminator escape character (\u000A) is used in Java, it terminates the string literal. In Java, this leads to an error, because line terminators are not allowed in string literals. You must use \n for a line feed in a string literal. In JavaScript, the escape sequence works the same way as \n.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1009690"> </A>
<A NAME="Displaying Characters with Unicode"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Displaying Characters with Unicode</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1009691"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can use Unicode to display the characters in different languages or technical symbols. For characters to be displayed properly, a client such as Netscape Navigator 4.x or Netscape 6 needs to support Unicode. Moreover, an appropriate Unicode font must be available to the client, and the client platform must support Unicode. Often, Unicode fonts do not display all the Unicode characters. Some platforms, such as Windows 95, provide a partial support for Unicode.</zparagraph>
</FONT>
<P>

<A NAME="1009692"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>To receive non-ASCII character input, the client needs to send the input as Unicode. Using a standard enhanced keyboard, the client cannot easily input the additional characters supported by Unicode. Sometimes, the only way to input Unicode characters is by using Unicode escape sequences. </zparagraph>
</FONT>
<P>

<A NAME="1009699"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>For more information on Unicode, see the <A HREF="http://www.unicode.org">Unicode Consortium Web site<font face="fontsansserif" SIZE="+2" COLOR="red"></A></font> and The Unicode Standard, Version 2.0, published by Addison-Wesley, 1996.</zparagraph>
</FONT>
<P>



   </BLOCKQUOTE>
</BOOKCONTENT>

<FOOTERCONTENT>
   <BLOCKQUOTE>
 <BR>
            <b><a href="partcore.html">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Previous</a></FONT>&nbsp;&nbsp;&nbsp;&nbsp;</b>
<a href="contents.html">
<b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Contents</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="ix.html"><b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Index</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="expr.html">
<b>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">
Next
</a>
</FONT>
</b>&nbsp;&nbsp;&nbsp;&nbsp;
        <HR NOSHADE SIZE="1">


   <font face="Arial, Helvetica, sans-serif" SIZE="1">

Copyright &#169; 2000 <a href="http://www.netscape.com">Netscape Communications Corp.</a> All rights reserved.</FONT>
   </BLOCKQUOTE>
   <FONT FACE="Arial, Helvetica, sans-serif" SIZE="1">
   <P ALIGN="RIGHT">Last Updated <B>September 28, 2000</B>
   </FONT>
   <BR>

</FOOTERCONTENT>

</MAINCONTENT>

</BODY>


</HTML>